namespace CIS.UserCheck
{
    using CISLib;
    using System;
    using System.Data;
    using System.IO;
    using System.Text;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class CheckUserPageInterFace : Page
    {
        protected DropDownList AreaDdl;
        protected Label Label7;
        protected Label lbAreaDdl;
        protected Label lbprovinceDdl;
        protected Label lbQuarter;
        protected Label lbRoleDdl;
        protected LinkButton lkbDown;
        protected UserPrincipal Principal = null;
        protected DropDownList provinceDdl;
        protected DropDownList RoleDdl;
        protected Button SearchBtn;
        protected Button startCalculationBtn;
        protected DataGrid UserListDg;
        protected DropDownList YQDdl;

        private void CalculateUserKPI()
        {
            Province[] provinceAry = ManageProvince.GetAllProvince();
            for (int x = 0; x < provinceAry.Length; x++)
            {
                Company[] companyAry = ManageCompany.GetCompanyAryViaId(provinceAry[x].ProvinceId);
                int userCompanyInfoCount = 0;
                for (int j = 0; j < companyAry.Length; j++)
                {
                    int emptyCount = 0;
                    Company companyInfo = companyAry[j];
                    emptyCount = (companyInfo.CompanyAdd == "") ? emptyCount : (emptyCount + 1);
                    emptyCount = (companyInfo.CompanyTel == "") ? emptyCount : (emptyCount + 1);
                    emptyCount = (companyInfo.CompanyFax == "") ? emptyCount : (emptyCount + 1);
                    emptyCount = (companyInfo.CompanyWebSite == "") ? emptyCount : (emptyCount + 1);
                    emptyCount = ManageCheckUser.IsExistOrgStructInCompanyInfo(companyAry[j].CompanyId) ? (emptyCount + 1) : emptyCount;
                    userCompanyInfoCount += emptyCount;
                }
                int IsUploadCompanyAdd = 0;
                int userFillInShareInfoCount = 0;
                if (companyAry.Length != 0)
                {
                    Guid provinceId = provinceAry[x].ProvinceId;
                    Guid uid = new Guid();
                    if (provinceId != uid)
                    {
                        if (ManageCheckUser.IsExistUploadInfoInFileStorageInfoViaDirectoryId(ManageCheckUser.GetDirectoryIdViaProvinceId(provinceId)))
                        {
                            IsUploadCompanyAdd = 1;
                        }
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "HotelProvince", "TB_ResourceHotel");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "RestaurantProvince", "TB_ResourceRestaurant");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "EntertainmentProvince", "TB_ResourceEntertainment");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "AgenciesProvince", "TB_ResourceAgencies");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "HospitalProvince", "TB_ResourceHospital");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "HolidayProvince", "TB_ResourceHoliday");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "EventsProvince", "TB_ResourceEvents");
                        userFillInShareInfoCount += ManageCheckUser.GetShareInfoCountInResourceInfo(provinceId, "TelephoneProvince", "TB_ResourceTelephone");
                    }
                }
                int userCompanyNumAllCount = companyAry.Length;
                double fillInInfoCount = 0.0;
                if (companyAry.Length > 0)
                {
                    fillInInfoCount = Math.Round(Convert.ToDouble(userCompanyInfoCount), 2);
                }
                ManageCheckUser.UpdateUserKPI_Interface(provinceAry[x].ProvinceId, this.YQDdl.SelectedItem.Text, fillInInfoCount, userCompanyNumAllCount, IsUploadCompanyAdd, userFillInShareInfoCount);
            }
        }

        private void CopyCompanyIdToKPI()
        {
            string selectYQ = this.YQDdl.SelectedItem.Text;
            ManageCheckUser.RemoveUserIdInKPI_Interface(selectYQ);
            ManageCheckUser.CopyUserIdToKPIViaRoleId(selectYQ, new Guid(this.RoleDdl.SelectedValue));
        }

        protected string getCompanyInfoCount(object obj)
        {
            int i = (int) obj;
            i *= 6;
            return i.ToString();
        }

        protected string GetCompanyName(object obj)
        {
            Guid provinceId = (Guid) obj;
            return ManageProvince.GetProvinceViaId(provinceId).ProvinceName;
        }

        private void InitializeComponent()
        {
            this.startCalculationBtn.Click += new EventHandler(this.startCalculationBtn_Click);
            this.SearchBtn.Click += new EventHandler(this.SearchBtn_Click);
            this.lkbDown.Click += new EventHandler(this.lkbDown_Click);
            this.UserListDg.PageIndexChanged += new DataGridPageChangedEventHandler(this.UserListDg_PageIndexChanged);
            base.Load += new EventHandler(this.Page_Load);
        }

        private void InitPage()
        {
            this.AreaDdl.Items.Clear();
            Area[] area = ManageArea.GetAreaAryViaId(this.Principal.SysUser.UserId);
            for (int i = 0; i < area.Length; i++)
            {
                ListItem tmpListItem = new ListItem();
                tmpListItem.Text = area[i].AreaName;
                tmpListItem.Value = area[i].AreaId.ToString();
                this.AreaDdl.Items.Add(tmpListItem);
            }
            this.provinceDdl.DataSource = ManageProvince.GetProvince(new Guid(this.AreaDdl.SelectedValue));
            this.provinceDdl.DataTextField = "RegionName";
            this.provinceDdl.DataValueField = "RegionId";
            this.provinceDdl.DataBind();
            Role role1 = ManageRole.GetRoleViaId(new Guid("A886157D-8484-4421-AC78-E55C8D0B44BE"));
            Role role2 = ManageRole.GetRoleViaId(new Guid("2162153A-5223-438F-A696-268E9B8EB77B"));
            this.RoleDdl.Items.Clear();
            ListItem role1Item = new ListItem(role1.RoleName, role1.RoleId.ToString());
            ListItem role2Item = new ListItem(role2.RoleName, role2.RoleId.ToString());
            this.RoleDdl.Items.Add(role1Item);
            this.RoleDdl.Items.Add(role2Item);
        }

        private void lkbDown_Click(object sender, EventArgs e)
        {
            DataTable dt = ManageCheckUser.GetKPI_Interface().Tables[0];
            StringWriter sw = new StringWriter();
            for (int ii = 0; ii < dt.Columns.Count; ii++)
            {
                if (ii == (dt.Columns.Count - 1))
                {
                    sw.Write(dt.Columns[ii].ColumnName + "\n");
                }
                else
                {
                    sw.Write(dt.Columns[ii].ColumnName + ",");
                }
            }
            for (int ii = 0; ii < dt.Rows.Count; ii++)
            {
                DataRow dr = dt.Rows[ii];
                for (int jj = 0; jj < dt.Columns.Count; jj++)
                {
                    if (jj == (dt.Columns.Count - 1))
                    {
                        sw.Write(dr[dt.Columns[jj].ColumnName] + "\n");
                    }
                    else
                    {
                        sw.Write(dr[dt.Columns[jj].ColumnName] + ",");
                    }
                }
            }
            sw.Close();
            base.Response.AddHeader("Content-Disposition", "attachment; filename=CustomerInfo" + DateTime.Now.Millisecond.ToString() + ".csv");
            base.Response.ContentType = "application/ms-excel";
            base.Response.ContentEncoding = Encoding.GetEncoding("GB2312");
            base.Response.Write(sw);
            base.Response.End();
        }

        protected override void OnInit(EventArgs e)
        {
            this.InitializeComponent();
            base.OnInit(e);
            this.Principal = this.Context.User as UserPrincipal;
        }

        private void Page_Load(object sender, EventArgs e)
        {
            if (this.Principal.SysUser.IsAdmin == 0)
            {
                throw new ApplicationException(string.Format("User: {0} not has privilege to access this page.", this.Principal.SysUser.LoginName));
            }
            if (!base.IsPostBack)
            {
                this.UserListDg.PageSize = 14;
                this.YQDdl.DataSource = KPIHelper.BuildLastFourYearQuarters(4);
                this.YQDdl.DataBind();
                this.SetFrom();
                this.InitPage();
                this.SetFromImage();
            }
        }

        private void SearchBtn_Click(object sender, EventArgs e)
        {
            this.UserListDg.CurrentPageIndex = 0;
            this.UserListDgDataBind();
        }

        private void SetFrom()
        {
            string language = this.Session["language"].ToString();
            string path = base.Server.MapPath("~/LanguageConfig.xml");
            this.lbAreaDdl.Text = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "lbAreaDdl", language, path);
            this.lbprovinceDdl.Text = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "lbprovinceDdl", language, path);
            this.lbRoleDdl.Text = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "lbRoleDdl", language, path);
            this.lbQuarter.Text = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "lbQuarter", language, path);
            this.UserListDg.Columns[0].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "Name", language, path);
            this.UserListDg.Columns[1].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "FillInInfoCount", language, path);
            this.UserListDg.Columns[2].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "InfoCount", language, path);
            this.UserListDg.Columns[3].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "FillInCompanyCount", language, path);
            this.UserListDg.Columns[4].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "IsUploadCompanyAddList", language, path);
            this.UserListDg.Columns[5].HeaderText = ManageSystemConfig.GetSystemSetLanguage("CheckUserPageInterFace", "FillInShareCount", language, path);
        }

        private void SetFromImage()
        {
        }

        private void startCalculationBtn_Click(object sender, EventArgs e)
        {
            this.CopyCompanyIdToKPI();
            this.CalculateUserKPI();
        }

        private void UserListDg_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.UserListDg.CurrentPageIndex = e.NewPageIndex;
            this.UserListDgDataBind();
        }

        private void UserListDgDataBind()
        {
            this.UserListDg.DataSource = ManageCheckUser.GetKPI_Interface();
            this.UserListDg.DataBind();
        }
    }
}

